﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FrayTank2 : ControllerBase
{
    public override PlayerData GetPlayerData()
    {
        return new PlayerData()
        {
            Name = "谢尔曼M2",
            Author = "Fray",
            BaseImage = "Sprite/Base/Base10",
            GunImage = "Sprite/Gun/Gun07"
        };
    }

    private bool Looping = true;
    private int direction = 0;
    public override IEnumerator LoopAction()
    {
        while(Looping)
        {
            Move(1, (90) * (direction) + Random.Range(1, 45), Random.Range(2.0f, 4.0f));
            Turn(Time.realtimeSinceStartup * 100, 100);
            yield return new WaitForSeconds(1);
        }
        yield return 0;
    }
    private Coroutine ScanC;
    public override void OnScanOthers(ScanOthersData scanOthersData)
    {
        if (scanOthersData.IsEnemy)
        {
            if (ScanC != null)
                CoroutineMono.StopCoroutine(ScanC);
            ScanC = CoroutineMono.StartCoroutine(ScanOthers(scanOthersData));
        }
       
        
    }
    private IEnumerator ScanOthers(ScanOthersData scanOthersData)
    {
         float dx = scanOthersData.othersPosition.x;
         float dy = scanOthersData.othersPosition.y;
         float distance = (float)System.Math.Sqrt((Data.currentPosition.x - dx) * (Data.currentPosition.x - dx) + (Data.currentPosition.y - dy) * (Data.currentPosition.y - dy));

        Looping = false;
            if (distance < 5)
            {
                TurnTo(100, scanOthersData.othersPosition);
                CoroutineMono.StartCoroutine(charge());
                Looping = true;
                yield return new WaitForSeconds(2);
            }
        else
        {
            TurnTo(100, scanOthersData.othersPosition);
            int x = Random.Range(1,5);
            Fire(x, (int)(Data.currentEnergy / x));
            Talk("哇喔", 2f);
        }
        Looping = true;

        yield return new WaitForSeconds(2);
    }
    IEnumerator charge()
    {
        yield return new WaitForSeconds(0);
        Fire(1,5);
        
    }
    public override void OnScaned(float angle)
    {
        Move(1, angle + 90, Random.Range(0, 3));
        Talk("看，飞碟", 2f);
    }

    public override void OnCrashWall()
    {

        //Move(1, (Data.currentAngle +180)%360 ,Random.Range(3f,5f));
        direction = (direction + 1) % 4;
    }

    public override void Start()
    {
        base.Start();
        Looping = true;
        CoroutineMono.StartCoroutine(LoopAction());
        Talk("看我一手正方形打野之边缘OB打得你头皮发麻", 4f);
    }

    public override void OnCrashOthers(Vector3 othersPosition, float othersSpeed)
    {

    }

    public override void OnHitted(float bulletAngle)
    {
        Move(1,90+ bulletAngle,Random.Range(2,5));
        Talk("啊~不要~不要啊~", 1f);
    }


}